package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class LabelService {
    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;

    public List<Label> findAll() {

        return labelDao.findAll();
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    public Page<Label> findPage(int page, int size) {
        PageRequest pageRequest = new PageRequest(page-1, size);
        return labelDao.findAll(pageRequest);
    }

    public Page<Label> findSearch(Map whereMap, int page, int size) {
        Specification<Label> specification = where(whereMap);
        PageRequest pageRequest = new PageRequest(page-1, size);
        return labelDao.findAll(specification, pageRequest);
    }

    private Specification<Label> where(Map searchMap) {

        return new Specification<Label>() {

            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 标签ID
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                    predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 标签名称
                if (searchMap.get("labelname")!=null && !"".equals(searchMap.get("labelname"))) {
                    predicateList.add(cb.like(root.get("labelname").as(String.class), "%"+(String)searchMap.get("labelname")+"%"));
                }
                // 状态
                if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))) {
                    predicateList.add(cb.like(root.get("state").as(String.class), "%"+(String)searchMap.get("state")+"%"));
                }
                // 是否推荐
                if (searchMap.get("recommend")!=null && !"".equals(searchMap.get("recommend"))) {
                    predicateList.add(cb.like(root.get("recommend").as(String.class), "%"+(String)searchMap.get("recommend")+"%"));
                }

                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }


        //根据id查询
    public Label findOne(String id) {
        return labelDao.findById(id).get();
    }
    //添加
    public void add(Label label) {
        label.setId(idWorker.nextId()+""); //主键值
        labelDao.save(label);
    }
    //修改
    public void update(Label label) {
        labelDao.save(label);
    }
    //根据id删除
    public void delete(String id) {
        labelDao.deleteById(id);
    }
    //批删
    public void deleteList(String[] ids) {
        for (String id : ids) {
            labelDao.deleteById(id);
        }
    }




    public List<Label> findByRecommendOrderByCountDesc() {
        return labelDao.findByRecommendOrderByCountDesc("是");
    }

    public void addFans(String id) {
        labelDao.addFans(id);

    }
}
